package com.yun.platform.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.platform.excption.BizException;
import com.yun.platform.sys.mapper.UserMapper;
import com.yun.platform.sys.entity.User;
import com.yun.platform.sys.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Resource
    private UserMapper usermapper;

    /**
     * 通过电话查询
     * @param phone
     * @return
     */
    @Override
    public Long findByPhone (String phone) {
        User user = new User();
        user.setPhone(phone);
        return usermapper.selectCount(new QueryWrapper<User>().ge("phone",phone));
    }

    /**
     * 注册
     * @param usr
     * @return
     */
    @Override
    public Integer reg (User usr) {
         return usermapper.insert(usr);
    }

    @Override
    public User findMydata (int id) {
        return null;
    }

    @Override
    public User findLogin (String phone) {
      /*  User user = new User();
        Map<String,Object> map = new HashMap <>();
        user.setPhone(phone);*/
      //  QueryWrapper<User> query = new QueryWrapper<>();
        User user1 = usermapper.selectOne(new QueryWrapper<User>().ge("phone",phone));
        return user1;
    }

    /**
     * 用户修改接口
     * @param user
     * @return
     */
    @Override
    public User updateUser (User user) {
        User user1 = new User();
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",user.getId());

        int i = usermapper.update(user,updateWrapper);
        if(i == 0){
            throw new BizException("修改失败");
        }
        user1.setId(user.getId());
        User user2 = usermapper.selectById(user.getId());

        return user2;
    }

    @Override
    public boolean saveOrUpdate(User entity) {
        return false;
    }


}
